<?php

/**
* alltosun.com 核销记录列表接口 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http:// www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 高昂 (gaoa@alltosun.com) $
* $Date: 2015-7-3 上午9:42:33 $
* $Id$
*/

class message_center_widget
{
    private $per_page = 20;
    
    /**
     * 获取指定分类下的所有消息数
     */
    public function get_message_num_by_res_name($user_id, $res_name = '')
    {
        if (!$user_id) {
        	return false;
        }
        
        if (!$res_name || !isset(message_center_config::$res_name_list[$res_name])) {
        	return false;
        }
        $filter = array();
        $filter['user_id'] = $user_id;
        $filter['res_name'] = $res_name;
        return _model('message_user')->getTotal($filter);
        
    }
    
    
    /**
    * 获取消息总览
    */
    public function get_message_center_list($user_id)
    {
        if (!$user_id) {
            return false;
        }

        $message_center_list = array();

        foreach (message_center_config::$res_name_list as $res_name => $res_ch_name) {
            $message_center_list[$res_name]['res_name'] = $res_name;
            $message_center_list[$res_name]['res_ch_name'] = $res_ch_name;

            //获取当前分类下的未读消息数
            $not_read_num = message_center_helper::get_unread_message_num($user_id, $res_name);

            $message_center_list[$res_name]['not_read_num'] = $not_read_num;

            switch ($res_name) {
                case 'arrival':
                    $total_num = $this->get_message_num_by_res_name($user_id, $res_name);
                    if ($not_read_num) {
                        $message_center_list[$res_name]['content'] = $message_center_list[$res_name]['not_read_num'].'条新到货通知';
                    } elseif ($total_num) {
                        $message_center_list[$res_name]['content'] = '共'.$total_num.'条到货通知';
                    } else {
                        $message_center_list[$res_name]['content'] = '暂无到货通知';
                    }
                    break;
                case 'group':
                    $total_num = $this->get_message_num_by_res_name($user_id, $res_name);
                    if ($not_read_num) {
                    	$message_center_list[$res_name]['content'] = $message_center_list[$res_name]['not_read_num'].'条未读核销通知';
                    } elseif ($total_num) {
                        $message_center_list[$res_name]['content'] = '共'.$total_num.'条核销通知';
                    } else {
                        $message_center_list[$res_name]['content'] = '暂无核销通知';
                    }
                    break;
                case 'score':
                    $total_num = $this->get_message_num_by_res_name($user_id, $res_name);
                    if ($not_read_num) {
                    	$message_center_list[$res_name]['content'] = $message_center_list[$res_name]['not_read_num'].'条积分变动通知';
                    } elseif ($total_num) {
                        $message_center_list[$res_name]['content'] = '共'.$total_num.'条积分通知';
                    } else {
                        $message_center_list[$res_name]['content'] = '暂无积分通知';
                    }
                    break;
                case 'event':
                    $total_num = $this->get_message_num_by_res_name($user_id, $res_name);
                    if ($not_read_num) {
                    	$message_center_list[$res_name]['content'] = $message_center_list[$res_name]['not_read_num'].'条活动通知';
                    } elseif ($total_num) {
                        $message_center_list[$res_name]['content'] = '共'.$total_num.'条活动通知';
                    } else {
                        $message_center_list[$res_name]['content'] = '暂无活动通知';
                    }
                    break;
                case 'notice':
                    $total_num = $this->get_message_num_by_res_name($user_id, $res_name);
                    if ($not_read_num) {
                    	$message_center_list[$res_name]['content'] = $message_center_list[$res_name]['not_read_num'].'条公告通知';
                    } elseif ($total_num) {
                        $message_center_list[$res_name]['content'] = '共'.$total_num.'条公告通知';
                    } else {
                        $message_center_list[$res_name]['content'] = '暂无公告通知';
                    }
                    break;
                default:
                    $message_center_list[$res_name]['content'] = $message_center_list[$res_name]['not_read_num'].'条未读消息';
                    break;
            }
            
            // 获取对应分类下的最后一条消息
            $last_msg = array();
            $last_msg_user_info = array();
            // 获取此用户的最后一条信息id
            $last_msg_user_info = _model('message_user')->getList(array('user_id'=>$user_id, 'status'=>1, 'res_name'=>$res_name), 'ORDER BY `id` DESC Limit 0,1');
            if ($last_msg_user_info) {
            	$last_msg = _uri('message_center', $last_msg_user_info[0]['msg_id']);
            }
            
            if ($last_msg) {
                $message_center_list[$res_name]['add_time'] = @$last_msg['add_time'];
            } else {
                $message_center_list[$res_name]['add_time'] = '';
            }
        }
        return $message_center_list;
    }


    
    

    /**
    * 获取对应消息分类下的消息列表
    * @param $user_id
    * @param $res_name
    * @param $page_no
    */
    public function get_message_list($user_id, $res_name = '', $page_no = 1)
    {
        if (!$user_id || !isset(message_center_config::$res_name_list[$res_name])) {
        	return false;
        }
        // 为了接受推送后的跳转页面需要在进行一次插入操作
        message_center_helper::insert_message($user_id);
        
        $message_user_filter = array();
        $message_user_filter['user_id']  = $user_id;
        $message_user_filter['status']   = 1;
        $message_user_filter['res_name'] = $res_name;
        
        // 分页
        $total_count = _model('message_user')->getTotal($message_user_filter);
        if (!$total_count) {
            return false;
        }
        $pager = new Pager($this->per_page);
        // 分配页数
        if ($pager->generate($total_count)) {
            // 计算总页数
            $pages = $pager->getPages();

            //页数的越界判断
            if ($page_no < 1) {
                return array();
            }
            if ($page_no > $pages) {
                return array();
            }

        } else {
            $pages = 1;
        }
        
        $message_center_filter = array();
        $message_center_filter['res_name'] = $res_name;
        $message_center_filter['status'] = 1;
        $message_center_filter['id'] = _model('message_user')->getFields('msg_id', $message_user_filter);
        
        $message_list = _model('message_center')->getList($message_center_filter, 'ORDER BY `id` DESC '.$pager->getLimit($page_no));
        $msg_id_list  = _model('message_center')->getFields('id', $message_center_filter, 'ORDER BY `id` DESC '.$pager->getLimit($page_no));
        
        $day_list = array();
        $rec_list = array();

        foreach ($message_list as $k=>$message)
        {
            //  获取日期截止到天
            $cur_day = explode(' ', $message['add_time']);
            if (!in_array($cur_day[0], $day_list)) {
                array_push($day_list, $cur_day[0]);
            }
        }
        
        $rec_list = array();
        $rec_list['page'] = array();
        $rec_list['page']['pages'] = $pages;
        $rec_list['page']['page_no'] = $page_no;
        
        
        foreach ($day_list as $k=>$date) {
            // 日期
            $rec_list['data'][$k]['date'] = $date;
            
            // 计算条数
            $date_filter = array();
            $date_filter['status'] = 1;
            $date_filter['`add_time` <= '] = $date.' 23:59:59';
            $date_filter['`add_time` >= '] = $date.' 00:00:00';
            $date_filter['`id`'] = $msg_id_list;
            $rec_num = _model('message_center')->getTotal($date_filter, 'ORDER BY `id` DESC');
            $rec_list['data'][$k]['record_num'] = $rec_num;
            $msg_list = array();
            $msg_list = _model('message_center')->getList($date_filter, 'ORDER BY `id` DESC');
            
            foreach ($msg_list as $key => $val) {
                $res_list['data'][$k]['record_list'][$key]['id'] = $val['id'];
            
                if ($res_name == 'event') {
                    
                    $rec_list['data'][$k]['record_list'][$key]['preview_pic'] = _image($val['preview_pic'], 'big');
                    $rec_list['data'][$k]['record_list'][$key]['url']   = AnUrl('event/event_detail')."?id=".$val['id'];
                }
            
                if ($res_name == 'notice') {
                    $rec_list['data'][$k]['record_list'][$key]['url']   = AnUrl('message_center/notice_detail')."?id=".$val['id'];
                }
                $rec_list['data'][$k]['record_list'][$key]['title'] = $val['title'];
            
                $rec_list['data'][$k]['record_list'][$key]['add_time'] = $val['add_time'];
            
            
                //更改信息为已读
                if ($res_name != 'notice' && $res_name != 'event') {
                    message_center_helper::change_to_isread($msg_id_list, $user_id);
                    
                }
                
                // 返回消息的未读状态(揭开注释就可用)
                // $rec_list['data'][$k]['record_list'][$key]['is_read'] = _model('message_user')->getFields('is_read', array('msg_id'=>$val['id'], 'user_id'=>$user_id))[0];
            
            }
            
        } 
        
        return $rec_list;
        
    }
    
    /**
    * 获取消息详情
    */
    public function get_message_content($user_id, $msg_id)
    {
        if (!$msg_id || !$user_id) {
            return false;
        }

        $message_info = _uri('message_center', $msg_id);

        if ($message_info) {
            //判断此消息的推送范围
            if ($message_info['push_range'] == 0) {
                //在消息用户关系表中插入一条
                $info = array();
                $info['user_id'] = $user_id;
                $info['msg_id']  = $msg_id;
                $info['is_read'] = 1;

                _model('message_user')->create($info);

            }
            return $message_info;
        } else {
            return false;
        }

    }


}

?>